import 'dart:math';

import 'package:better_player_plus/better_player_plus.dart';
import 'package:yqmssa_app/router/page_router.dart';
import 'package:yqmssa_app/utils/shared_utils.dart';
import 'package:yqmssa_app/utils/tencent_im_utils.dart';
import 'package:yqmssa_app/views/index/request/banner_data_model.dart';
import 'package:yqmssa_app/views/index/request/index_request.dart';
import 'package:get/get.dart';

import 'home_sheet_screen.dart';
import 'message_manager.dart';

class HomeManager extends GetxController {
  @override
  void onInit() {
    super.onInit();
    getLun();
    getVl();
    serviceContent();
  }

  final bannerData = <LunDM>[].obs;

  final videosData = <VideoListDM>[].obs;

  final serviceData = "".obs;

  final betterPlayerController = BetterPlayerController(const BetterPlayerConfiguration()).obs;

  void getLun() {
    IndexRequest.bL().then((value) {
      bannerData.clear();
      bannerData.addAll(value);
    });
  }

  void getVl() {
    IndexRequest.vL().then((value) {
      videosData.clear();
      videosData.addAll(value);
    });
  }

  void serviceContent() {
    IndexRequest.serviceContent().then((onValue) {
      serviceData.value = onValue;
    });
  }

  void showSheet({int pushType = 1}) {
    if (sharedUtils.service != null) {
      gotoChat();
      return;
    }
    final index = Random().nextInt(videosData.length);
    if (pushType == 2) {
      Get.bottomSheet(HomeSheetScreen(id: videosData[index].id), isScrollControlled: true);
    } else {
      Get.toNamed(PageRouter.landing, parameters: {"id": "${videosData[index].id}"});
    }
  }

  ///跳转微信并且跳转聊天
  void gotoChat() {
    tencentImUtils.pushPage();
    Get.find<MessageManager>().getSystemMsg();
  }
}
